# 可在CPU/GPU环境运行
from modelscope.models import Model
from modelscope.pipelines import pipeline
# Version less than 1.1 please use TextRankingPreprocessor
from modelscope.preprocessors import TextRankingTransformersPreprocessor
from modelscope.utils.constant import Tasks

model_id = 'damo/nlp_corom_passage-ranking_english-base'
model = Model.from_pretrained(model_id)
preprocessor = TextRankingTransformersPreprocessor(model.model_dir)
pipeline_ins = pipeline(task=Tasks.text_ranking, model=model, preprocessor=preprocessor)

def SemanticMatch(field1: str, field2: str):
    input = {
        'source_sentence': [field1],
        'sentences_to_compare': [field2]
    }
    result = pipeline_ins(input=input)
    return result  # {'scores': [0.14329610764980316]}
